package PriorityQueueTOPK小;

import java.util.Arrays;
import java.util.PriorityQueue;

class Student{}
public class Test {
    /*public static void main(String[] args) {
        PriorityQueue<Integer> priorityQueue=new PriorityQueue<>();
        //默认情况下是一个小根堆
        priorityQueue.offer(10);
        priorityQueue.offer(5);
        priorityQueue.offer(9);
        priorityQueue.offer(7);
        priorityQueue.offer(77);

        System.out.println(priorityQueue.poll());
        System.out.println(priorityQueue.poll());
        System.out.println(priorityQueue.poll());
        System.out.println(priorityQueue.poll());
        System.out.println(priorityQueue.poll());

        *//*
        PriorityQueue中放置的元素必须要能够比较大小，不能插入无法比较大小的对象，否则会抛出ClassCastException异常

        PriorityQueue<Student> priorityQueue1=new PriorityQueue<>();
        priorityQueue1.offer(new Student());//放一个不报错
        priorityQueue1.offer(new Student());//再放一个就报错咯
        *//*




    }*/

    //Top-K问题 :找最大的 K 个数
    public static int[] maxestK(int[] array, int k){

        if(array==null || k==0){
            return new int[0];
        }
        PriorityQueue<Integer> p=new PriorityQueue<>();

        int[] ret=new int[k];
        for (int i = 0; i < k; i++) {
            p.offer(array[i]);
        }

        for (int i = 0; i < array.length; i++) {
            int top= p.peek();
            if(array[i]>top){
                p.poll();
                p.offer(array[i]);
            }
        }

        for (int i = 0; i < k; i++) {
            ret[i]=p.poll();
        }

        return ret;
    }

    public static void main(String[] args) {
        int[] array={10,4,2,6,15,24,9,0,4};
        int[] ret=maxestK(array,3);
        System.out.println(Arrays.toString(ret));
    }

















}
